package cn.ac.aircas.web.controller.system;

import cn.ac.aircas.common.annotation.Anonymous;
import cn.ac.aircas.common.annotation.Cache;
import cn.ac.aircas.common.core.controller.BaseController;
import cn.ac.aircas.common.core.domain.R;
import cn.ac.aircas.common.exception.GlobalException;
import cn.ac.aircas.system.domain.vo.RoadConditionPointVO;
import cn.ac.aircas.system.domain.vo.RoadConditionTabvo;
import cn.ac.aircas.system.domain.vo.RoadConditionvo;
import cn.ac.aircas.system.service.IRoadService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static cn.ac.aircas.common.constant.Constants.EXCEPTION_DETAIL_TYPE;

@RestController
@RequestMapping("/roadCondition")
@Api(tags = "路况请求处理")
public class RoadConditionController extends BaseController {

    @Autowired
    IRoadService roadService;

    @Cacheable(value = "ground", key = "#param+'getRoadList_'")
    @ApiOperation("查询城市地图道路路况信息")
//    @Anonymous
    @RequestMapping(value = "/getRoadList", method = RequestMethod.POST)
    public R<RoadConditionvo> getRoadList(@RequestBody(required = false) Map param) {
        RoadConditionvo vo = new RoadConditionvo();
        List<RoadConditionTabvo> listTab = new ArrayList<>();
        List<RoadConditionPointVO> listPoint = new ArrayList<>();
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        try {
            if (param.get(EXCEPTION_DETAIL_TYPE) != null) {
                if (param.get(EXCEPTION_DETAIL_TYPE).toString().equals("7")) {
                    listTab = CompletableFuture.supplyAsync(() ->
                            roadService.getRoadList(param), executorService).get();
                    listPoint = CompletableFuture.supplyAsync(() ->
                            roadService.getRoadPointList(param), executorService).get();
                } else {
                    listTab = CompletableFuture.supplyAsync(() ->
                            roadService.getRoadList(param), executorService).get();
                }
            } else {
                listTab = CompletableFuture.supplyAsync(() ->
                        roadService.getRoadList(param), executorService).get();
            }
        } catch (Exception e) {
            throw new GlobalException();
        } finally {
            executorService.shutdown();
        }
        vo.setListTab(listTab);
        vo.setListPoint(listPoint);
        return R.ok(vo);
    }

    /**
     * 查询路面检测概况
     */
    @ApiOperation("查询路面检测概况")
    @PostMapping("/statRoadConditions/{cityName}")
    @Cacheable(value = "ground", key = "#cityName+'statRoadConditions_'")
//    @Anonymous
    public R statRoadConditions(@PathVariable(required = false) String cityName) {
        Map<String, BigDecimal> map = roadService.statRoadConditions(cityName);
        return R.ok(map,"查询成功");
    }

    /**
     * 查询路面状态指标
     */
    @ApiOperation("查询路面状态指标")
    @PostMapping("/getRoadConditionStat/{cityName}")
    @Cacheable(value = "ground", key = "#cityName+'getRoadConditionStat_'")
    @Anonymous
    public R getRoadConditionStat(@PathVariable String cityName) {
        Map stat = roadService.getRoadConditionStat(cityName);
        return R.ok(stat);
    }

    @ApiOperation("查询路面状态指标详细信息")
    @PostMapping("/getRoadConditionStatDetail")
    @Cacheable(value = "ground", key = "#param+'getRoadConditionStatDetail_'")
//    @Anonymous
    public R getRoadConditionStatDetail(@RequestBody(required = false) Map<String,Object> param) {
        List<RoadConditionTabvo> listTab = roadService.getRoadList(param);
        return R.ok(listTab);
    }

    /**
     * 查询道路异常实况
     */
    @ApiOperation("查询道路异常实况")
    @PostMapping("/getRoadAbnormalStat/{cityName}")
    @Cacheable(value = "ground", key = "#cityName+'getRoadAbnormalStat_'")
//    @Anonymous
    public R getRoadAbnormalStat(@PathVariable String cityName) {
        List<Map> roadAbnormalStat = roadService.getRoadAbnormalStat(cityName);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = null;
        try {
            json = objectMapper.writeValueAsString(roadAbnormalStat);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        try {
            roadAbnormalStat = objectMapper.readValue(json, new TypeReference<List<Map>>() {});
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return R.ok(roadAbnormalStat);
    }
}
